<template>
	<div class="user">
		<div class="product-content">
			<el-form ref="form" :model="form" :rules="formRules" label-width="150px">
				<Basic v-if="!loading"></Basic>
			</el-form>
			<!--提交-->
			<div class="common-button-wrapper">
				<el-button type="info" @click="gotoBack">返回</el-button>
				<el-button type="primary" @click="onSubmit" :loading="loading">提交</el-button>
			</div>
		</div>
		<!--上传图片组件-->
		<Upload v-if="isupload" :isupload="isupload" :type="type" @returnImgs="returnImgsFunc">上传图片</Upload>
		<!--选择优惠券-->
		<GetCoupon v-if="open_add" :open_add="open_add" @closeDialog="closeProductDialogFunc($event)">选择优惠券弹出层
		</GetCoupon>
	</div>
</template>
<script>
	import InvitationGiftApi from '@/api/invitationgift.js';
	import Basic from "./part/Basic.vue"
	export default {
		components: {
			/*选择优惠券件*/
			Basic
		},
		data() {
			return {
				form: {
					name: "",
					invCondition: 0,
					startTime: "",
					endTime: "",
					imageId: "",
					filePath: "",
					ruleId: "",
					ruleFilePath: "",
					invImageId: "",
					invImagePath: "",
					isShow: 1,
					shareImageId: "",
					shareTitle: "",
					shareDesc: "",
					invType: 0,
					invMember: 0,
					invConsumption: 0,
					/*奖励列表*/
					rewardData: [{
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					}],
					memberReward: {
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					},
					consumptionReward: {
						invitationNum: 1,
						isCoupon: false,
						point: 0,
						isPoint: false,
						couponIds: "",
						couponList: [],
						isBalance: false,
						balance: "",
					},
					invRule: ''
				},
				/*优惠券*/
				tableData: [],
				loading: true,
				open_add: false,
				/*奖励列表*/
				rewardData: [],
				coupon_index: 0,
				file_path: '',
				/*是否打开图片选择*/
				isupload: false,
				shareFilePath: '',
				formRules: {
					imageId: [{
						required: true,
						message: '请上传背景图',
						trigger: 'blur'
					}],
					shareImageId: [{
						required: true,
						message: '请上传分享图',
						trigger: 'blur'
					}]
				}
			};
		},
		provide: function() {
			return {
				form: this.form,
			};
		},
		created() {
			/*获取数据*/
			this.getData();
		},
		methods: {
			/*获取数据*/
			getData() {
				let self = this;
				let invitationGiftId = self.$route.query.invitationGiftId;
				InvitationGiftApi.getInvitation({
							invitationGiftId: invitationGiftId
						},
						true
					)
					.then(res => {
						res.data.rewardData = res.data.reward;
						Object.assign(self.form, res.data);
						console.log(self.form)
						self.loading = false;
					})
					.catch(error => {
						self.loading = false;
					});
			},
			/*提交表单*/
			onSubmit() {
				let self = this;
				if (self.form.rewardData.length == 0) {
					ElMessage.error('请添加奖项');
					return;
				}
				let form = self.form;
				form.reward = self.form.rewardData;
				self.$refs.form.validate(valid => {
					if (valid) {
						self.loading = true;
						InvitationGiftApi.editInvitation(form, true)
							.then(data => {
								self.loading = false;
								if (data.code == 1) {
									ElMessage({
										message: data.msg,
										type: 'success'
									});
									self.$router.push('/plus/invitation/index');
								} else {
									self.loading = false;
								}
							})
							.catch(error => {
								self.loading = false;
							});
					}
				});
			},
			/*返回上一页面*/
			gotoBack() {
				this.$router.back(-1);
			}
		}
	};
</script>